"""
功能描述 ：
编写人 ：
编写日期 ：
实现逻辑 ：
    1- ......
        1.1- ......
    2- ......
        2.1- ......
"""
"""try:
    可能发生错误的代码
except:
    如果出现异常执行标的代码 """
# try:
#     print(1/0)
# except ZeroDivisionError:
#     print('有错误')
#
# # # 异常的else
# try:
#     print(1)
# except Exception as result:
#     print(result)
# else:
#     print('没有异常的代码')
# # 异常的finally，是⽆论是否异常都要执⾏的代码
# try:
#     f = open('test.txt', 'r')
# except Exception as result:
#     f = open('test.txt', 'w')
# else:
#     print('没有异常')
# finally:
#     f.close()
# # """异常的传递
# # 需求：
# # 1. 尝试只读⽅式打开test.txt⽂件，如果⽂件存在则读取⽂件内容，⽂件不存在则提示⽤户即可。
# # 2. 读取内容要求：尝试循环读取内容，读取过程中如果检测到⽤户意外终⽌程序，则 except 捕获异常并提示⽤户
# import time
# try:
#     f = open('tect.txt')
#     try:
#         while True
#             content = f.readline()
#             if len(content) == 0:
#                 break
#             time.sleep(2)
#             print(content)
#     except:
#
# # 如果在读取⽂件的过程中，产⽣了异常，那么就会捕获到
#  # ⽐如 按下了 ctrl+c
#          print('意外终止读取数据')
#     finally:
#         f.close()
#         print('关闭文件')
# except:
#     print('没有这这个文件')

#自定义异常
# 密码⻓度不⾜，则报异常（⽤户输⼊密码，如果输⼊的⻓度不⾜3位，则报错，即抛出⾃定义异
# 常，并捕获该异常）
# ⾃定义异常类，继承Exception
# class ShortInputError(Exception):
#     def __iter__(self,length,min_len ):
#         self.length = length
#         self.min_len = min_len
#     #设置抛出异常的信息
#     def __str__(self):
#         return f'输入长度是{self.length},长度不能少于{self.min_len}'
# def main():
#     try:
#         con = input('请输入密码：')
#         if len(con)  < 3:
#             raise ShortInputError(len(con),3)
#     except Exception as result:
#         print(result)
#     else:
#         print('密码已经输⼊完成')
# main()

"""
面向对象
定义类：
class 类名:
    代码
创建对象：
    对象名 = 类名()
"""

# 添加和获取对象属性
# 1、类外面添加对象属性
# 对象名.属性名 = 值
# 2、类外面获取对象属性
# 对象名.属性名
# 3、类里面获取对象属性
# self.属性名

# 魔法方法
# __init__() 初始化对象 __str__()
# class Washer():
#     def __init__(self, width, height):
#         self.width = width
#         self.height = height
#     def print_info(self):
#          print(f'洗⾐机的宽度是{self.width}, ⾼度是{self.height}')
#     def __str__(self):
#         return '我是说明书'
# haier = Washer(111,222)
# print(haier)
# haier.print_info()
#
# # #__del__() 删除对象
# # class Washer():
# #     def __init__(self, width, height):
# #         self.width = width
# #         self.height = height
# #     def __del__(self):
# #         print(f'{self}对象已经被删除')
# # haier1 = Washer(10, 20)
# # # <__main__.Washer object at 0x0000026118223278>对象已经被删除
# # del haier1
#
# #烤地瓜
# class SweetPotato():
#     def __init__(self):
#         #时间
#         self.cook_time = 0
#         #烧烤状态
#         self.cook_static = '生的'
#         #调料列表
#         self.condiments = []
#     def cook(self,time):
#         """烤地⽠的⽅法"""
#         self.cook_time += time
#         if 0 <= self.cook_time < 3:
#             self.cook_static = '⽣的'
#         elif 3 <= self.cook_time < 5:
#             self.cook_static = '半⽣不熟'
#         elif 5 <= self.cook_time < 8:
#             self.cook_static = '熟了'
#         elif self.cook_time >= 8:
#             self.cook_static = '烤糊了'
#     def add_condiments(self, condiment):
#         """添加调料"""
#         self.condiments.append(condiment)
#     def __str__(self):
#         return f'这个地⽠烤了{self.cook_time}分钟, 状态是{self.cook_static}, 添加的调料有{self.condiments}'
#
# digua1 = SweetPotato()
# print(digua1)
# digua1.cook(2)
# digua1.add_condiments('酱油')
# print(digua1)
# digua1.cook(2)
# digua1.add_condiments('辣椒⾯⼉')
# print(digua1)
# digua1.cook(2)
# print(digua1)
# digua1.cook(2)
# print(digua1)
#
# #单继承
# #师傅类
# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香味 ]'
#
#     def make_cake(self):
#         print(f'利用 {self.kongfu}五香味煎饼果子')
# class Prentice(Master):
#     pass
# xiaoming = Prentice()
# # 4. 徒弟
# print(xiaoming.kongfu)
# #多继承
# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香味 ]'
#     def make_cake(self):
#         print(f'利用 {self.kongfu}五香味煎饼果子')
# class School(object):
#     def __init__(self):
#         self.kongfu = '[香辣 ]'
#     def make_cake(self):
#         print(f'利用 {self.kongfu}香辣味煎饼果子')
# class Prentice(Master,School):
#     pass
# xiaoming = Prentice()
# print(xiaoming.kongfu)
# xiaoming.make_cake()
#
# #⼦类重写⽗类同名⽅法和属性
# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香味 ]'
#     def make_cake(self):
#         print(f'利用 {self.kongfu}五香味煎饼果子')
# class School(object):
#     def __init__(self):
#         self.kongfu = '[香辣 ]'
#     def make_cake(self):
#         print(f'利用 {self.kongfu}香辣味煎饼果子')
# class Prentice(Master,School):
#     def __init__(self):
#         self.kongfu = '[自创 ]'
#     def make_cake(self):
#         print(f'利用 {self.kongfu}自创煎饼果子')
#
# xiaoming = Prentice()
# print(xiaoming.kongfu)
# xiaoming.make_cake()
# print(Prentice.__mro__)
# #⼦类调⽤⽗类的同名⽅法和属性
# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香味 ]'
#     def make_cake(self):
#         print(f'利用 {self.kongfu}五香味煎饼果子')
# class School(object):
#     def __init__(self):
#         self.kongfu = '[香辣 ]'
#     def make_cake(self):
#         print(f'利用 {self.kongfu}香辣味煎饼果子')
# class Prentice(Master,School):
#     def __init__(self):
#         self.kongfu = '[自创 ]'
#     def make_cake(self):
#         print(f'利用 {self.kongfu}自创煎饼果子')
#
#     def make_cake(self):
#         self.__init__()
#         print(f'运用 {self.kongfu}z制度煎饼果子')
#
#     def make_master_cake(self):
#         Master.__init__(self)
#         Master.make_cake(self)
#
#     def make_school_cake(self):
#         School.__init__(self)
#         School.make_cake(self)
# xiaoming = Prentice()
# xiaoming.make_cake()
# xiaoming.make_master_cake()
# xiaoming.make_school_cake()
# xiaoming.make_cake()
# #多层继承
# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香味 ]'
#     def make_cake(self):
#         print(f'利用 {self.kongfu}五香味煎饼果子')
# class School(object):
#     def __init__(self):
#         self.kongfu = '[香辣 ]'
#     def make_cake(self):
#         print(f'利用 {self.kongfu}香辣味煎饼果子')
# class Prentice(Master,School):
#     def __init__(self):
#         self.kongfu = '[自创 ]'
#     def make_cake(self):
#         print(f'利用 {self.kongfu}自创煎饼果子')
#
#     def make_cake(self):
#         self.__init__()
#         print(f'运用 {self.kongfu}z制度煎饼果子')
#
#     def make_master_cake(self):
#         Master.__init__(self)
#         Master.make_cake(self)
#
#     def make_school_cake(self):
#         School.__init__(self)
#         School.make_cake(self)
# class Tusun(Prentice):
#     pass
# xiaogang = Tusun()
# xiaogang.make_cake()
# xiaogang.make_school_cake()
# xiaogang.make_master_cake()

#super()调⽤⽗类⽅法

# class Master():   #师父类
#     def __init__(self):
#         self.kongfu = ['五香煎饼果子']
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作煎饼果子')
#
# class School():  #学校类
#     def __init__(self):
#         self.kongfu = ['香辣煎饼果子']
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作煎饼果子')
#         super.__init__()
#         super.make_cake()
# class Perntice(School,Master):
#
#     def __init__(self):
#          self.kongfu = '[独创煎饼果子 ]'
#     def make_cake(self):
#          self.__init__()
#          print(f'运用{self.kongfu}制作煎饼果子')
#
#     def make_master_cake(self):
#         Master.__init__(self)
#         Master.make_cake(self)
#     def make_school_cake(self):
#         School.__init__(self)
#         School.make_cake(self)
#     def make_old_cake(self):
#         super.__init__(self)
#         super.make_cake(self)
#
# xm = Perntice()
# xm.make_old_cake()

# 获取和修改私有属性值
# class Master():   #师父类
#     def __init__(self):
#         self.kongfu = ['五香煎饼果子']
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作煎饼果子')
#
# class School():  #学校类
#     def __init__(self):
#         self.kongfu = ['香辣煎饼果子']
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作煎饼果子')
#
# class Perntice(School,Master):
#     def __init__(self):
#         self.kongfu = '[独创煎饼果子 ]'
#         self.money = 2000000
#     #获取私有属性
#     def get_money(self):
#         return self.money
#     #修改私有属性
#     def set_money(self):
#         self.money = 500
#     def _info_print(self):
#         print(self.kongfu)
#         print(self._money)
#     def make_cake(self):
#         self.__init__()
#         print(f'运用{self.kongfu}制作煎饼果子')
#     def make_master_cake(self):
#         Master.__init__(self)
#         Master.make_cake(self)
#     def make_school_cake(self):
#         School.__init__(self)
#         School.make_cake(self)
# class Tusun(Perntice):
#     pass
#
# xm = Perntice()
# xg = Tusun()
# print(xg.get_money())
# xg.set_money()
# print(xg.get_money())

#  ⾯向对象三⼤特性
# 封装
# 将属性和⽅法书写到类的⾥⾯的操作即为封装
# 封装可以为属性和⽅法添加私有权限
# 继承
# ⼦类默认继承⽗类的所有属性和⽅法
# ⼦类可以重写⽗类属性和⽅法
# 多态
# 传⼊不同的对象，产⽣不同的结果

# class Dog():
#     def work(self):
#         print('指哪打哪')
# class ArmyDog(Dog):
#     def work(self):
#         print('缉毒犬')
# class DrugDog(Dog):
#     def work(self):
#         print('警犬')
# class Person()
#     def work_dog(self,dog):
#         dog.work()
# ad = ArmyDog()
# dd = DrugDog()
# xiao = Person()
# xiao.word_dog(ad)
# xiao.word_dog(dd)

# #类属性-修改类属性
# class Dog():
#     tooth = 10
# wangcai = Dog()
# Dog.tooth
#
# Dog.tooth = 12
# dahuang = Dog()
# print(dahuang.tooth)
# print(Dog.tooth)

# 静态⽅法特点
# 需要通过装饰器 @staticmethod 来进⾏修饰，静态⽅法既不需要传递类对象也不需要传递实例对象
# （形参没有self/cls）。
# 静态⽅法 也能够通过 实例对象 和 类对象 去访问。

class Dog():
    @staticmethod
    def info_print():
        print('这是⼀个狗类，⽤于创建狗实例....')

dh = Dog()
dh.info_print()
Dog.info_print()
